package tech.abing.spider.task.pageprocessor.amac;

import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.FundMngComEntity;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class FundMngComProcessor implements PageProcessor{

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		String url = page.getRequest().getUrl();
		Element table = doc.select("div.innerC.mlAuto > div.innerCenter > div.iRight > div.ldContent > div > table").first();
		Elements trs = table.select("tr:gt(0)");
		
		//首行样式比较特别，做特殊处理：
		FundMngComEntity fmc = new FundMngComEntity();
		fmc.setId(UuIdPublic.timeStrongID(5));
		Elements first_td = trs.get(0).select("td");
		fmc.setManagerName(first_td.get(2).text());
		fmc.setEstablishDate(first_td.get(3).text());
		fmc.setOfficialWebsite(first_td.get(4).text());
		fmc.setContactAddress(first_td.get(5).text());
		fmc.setTel(first_td.get(6).text());
		fmc.setManagerType(first_td.get(7).text());
		fmc.setUrl(url);
		StringUtils.removeSpace(fmc);
		objectContainer.add(fmc);
		trs.remove(0);
		for (Element tr : trs) {
			FundMngComEntity _fmc = new FundMngComEntity();
			_fmc.setId(UuIdPublic.timeStrongID(5));
			Elements tds = tr.select("td");
			_fmc.setManagerName(tds.get(1).text());
			_fmc.setEstablishDate(tds.get(2).text());
			_fmc.setOfficialWebsite(tds.get(3).text());
			_fmc.setContactAddress(tds.get(4).text());
			_fmc.setTel(tds.get(5).text());
			_fmc.setManagerType(tds.get(6).text());
			_fmc.setUrl(url);
			StringUtils.removeSpace(_fmc);
			objectContainer.add(_fmc);
		}
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
